using System;
using System.Collections.Generic;
using System.Linq;
using BLToolkit.Data.Linq;
using dbvc.data;

namespace dbvc.Queries
{
    public class UserQuery : GenericQuery<db_user>
    {
        private static readonly Func<DAA, int, IQueryable<db_user>> cGetProjUser =
            CompiledQuery.Compile((DAA db, int prj_id) =>
                          db.GetTable<db_user>().Where(
                              s => db.GetTable<db_user_project>()
                                       .Where(up => up.id_project == prj_id)
                                       .Select(ss => ss.id_user)
                                       .Contains(s.id)));

        private static readonly Func<DAA, int, IQueryable<db_user>> cGetNotProjUser =
            CompiledQuery.Compile((DAA db, int prj_id) =>
                          db.GetTable<db_user>().Where(
                              s => !db.GetTable<db_user_project>()
                                       .Where(up => up.id_project == prj_id)
                                       .Select(ss => ss.id_user)
                                       .Contains(s.id)));

        public static IList<db_user> GetUsers(int proj_id, bool contain)
        {
            using (var db = new DAA())
            {
                if (contain) return cGetProjUser(db, proj_id).ToList();
                return cGetNotProjUser(db, proj_id).ToList();
            }
        }
    }
}